// 插入排序
function insert (arr, n) {
  let key = arr[n]
  let i = n
  while (arr[i-1] > key) {
    arr[i] = arr[i-1]
    i--
    if (i===0) break
  }
  arr[i] = key
}

function insertSort (arr) {
  for (let i = 1; i < arr.length; i++) {
    insert(arr, i)
  }
  return arr
}

console.log(insertSort([4,5,9,6,1,2,8,3]))